package com.shoeHouse.commodityCategory.action;

import java.util.List;

import com.shoeHouse.commodityCategory.model.CommodityCategory;

public class CategoryTreeUtil {
	
	
	public String TreeUtil(List<CommodityCategory> list,String pid){
		
		
		StringBuilder builder=new StringBuilder();
		builder.append("[");
		CommodityCategory category;
		int j=0;
		for(int i=0;i<list.size();i++){
			category=list.get(i);
			
			if(category.getPid().equals(pid)){
				
				String children=TreeUtil(list,category.getId());
				if(j!=0){
					builder.append(",");
					
				}
			if(children.equals("[]")){
				builder.append("{\"id\":\""+category.getId()+"\",\"text\":\""+category.getName()+"\"}");
				
			}else{
			builder.append("{\"id\":\""+category.getId()+"\",\"text\":\""+category.getName()+"\",\"children\":"+children+"}");
			}
			j++;
			}
	
		
		}
		
		
		builder.append("]");
		return builder.toString();
	}

}
